package View;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.KeyStroke;
import javax.swing.ScrollPaneConstants;
import javax.swing.border.EmptyBorder;

import Control.DataConnection;
import Control.Zoomable;

@SuppressWarnings("serial")
public class MainApplication extends JFrame {

	/**
	 *	Calls the MainApplication constructor to build a new JFrame
	 * 	@param args	none
	 */
	public static void main(String[] args) {
		new MainApplication("Main Application iSplice | Undergraduate Research Project by Stephen Williamson");
		
	}
	
	public MainApplication (String title)
	{
		super(title);
		
		// Get maximum value for the application, and then set it to that.
		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
		this.setSize(dim);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
		// Unload the Database Connection...
		this.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent we) {
				unload();
				System.exit(0);
			}
		});
		
		Container content = this.getContentPane();
		content.setLayout(new BorderLayout()); 
		
		JPanel propertyPanel = VisualizationPropertiesPanel.getInstance().initPanel();
		JPanel visualizePanel = VisualizationPanel.getInstance();
		
		JScrollPane visualizeScrollPane = new JScrollPane(visualizePanel,
				ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER,
				ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
		visualizeScrollPane.setBorder(new EmptyBorder(0, 0, 10, 0));
		
		Zoomable.getInstance().registerComponent(visualizeScrollPane);
		
		content.add(visualizeScrollPane, BorderLayout.CENTER);
		content.add(propertyPanel, BorderLayout.EAST);
		content.validate(); // Calculate the width and height of the components
		
		Zoomable.getInstance().setWidth(visualizePanel.getWidth());
		this.createZoomCapabilities();
		
//		System.out.println ("Is Valid: " + content.isValid());
//		System.out.println ("Container: " + content.getWidth() + ", " + content.getHeight());
//		System.out.println ("Property Panel: " + propertyPanel.getWidth() + ", " + propertyPanel.getHeight());
//		System.out.println ("Visualize Panel: " + visualizePanel.getWidth() + ", " + visualizePanel.getHeight());
//		System.out.println ("---");
	}
	
	private void createZoomCapabilities() 
	{
		// Create Actions to be used in key presses
		Action zoomIn = new AbstractAction() {
			@Override
			public void actionPerformed(ActionEvent ev) {
				Zoomable.getInstance().zoomIn();
			}
		};
		
		Action zoomOut = new AbstractAction() {
			@Override
			public void actionPerformed(ActionEvent ev) {
				Zoomable.getInstance().zoomOut();
			}
		};
		
		Action autoZoom = new AbstractAction() {
			@Override
			public void actionPerformed(ActionEvent ev) {
				Zoomable.getInstance().resetZoom();
			}
		};
		
		// Create the Key Strokes - menu shortcut lets MAC use the apple key (command)
		KeyStroke keyZoomIn = KeyStroke.getKeyStroke(KeyEvent.VK_EQUALS,
				Toolkit.getDefaultToolkit().getMenuShortcutKeyMask());
		KeyStroke keyZoomOut = KeyStroke.getKeyStroke(KeyEvent.VK_MINUS,
				Toolkit.getDefaultToolkit().getMenuShortcutKeyMask());
		KeyStroke keyAutoZoom = KeyStroke.getKeyStroke(KeyEvent.VK_0,
				Toolkit.getDefaultToolkit().getMenuShortcutKeyMask());
		
		// Add the keys to the input map
		InputMap im = this.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
		
		im.put(keyZoomIn, zoomIn);
		im.put(keyZoomOut, zoomOut);
		im.put(keyAutoZoom, autoZoom);		
		
		// Add the key + actions into the action map of the RootPane
		this.getRootPane().getActionMap().put(im.get(keyZoomOut), zoomOut);
		this.getRootPane().getActionMap().put(im.get(keyZoomIn), zoomIn);
		this.getRootPane().getActionMap().put(im.get(keyAutoZoom), autoZoom);		
	}
	
	public void unload() {
		DataConnection.getInstance().closeConnection();
	}

}
